package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.DaduiGlDao;
import com.hbpa.form.DaduiJdyj;

import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;
 
public class DaduiGlDaoImpl implements DaduiGlDao {

	public boolean saveStXqbx(String xh,String xqbx) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_bxqk(xh,bx) values(?,?)");
			 
			ps.setString(1, xh);
			ps.setString(2, xqbx);
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return  false;
	}

	public boolean updateStXqbx(String xh,String xqbx) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("update st_bxqk set bx = ? where xh = "+xh);
			 
			ps.setString(1, xqbx);
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return  false;
	}

	public boolean checkXqbx(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_bxqk where xh = ?");
			ps.setString(1,xh);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public String[] getXqbxByXh(String xh) {
		if(checkXqbx(xh)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		String bxqk = "";
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_bxqk where xh = ? order by id asc");

			ps.setString(1, xh);
			rs = ps.executeQuery();
			if(rs.next())
			{    
				bxqk = rs.getString("bx"); 
			}
			 return bxqk.split(" ");
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}

	public boolean checkJdyj(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from dd_jdyj where xh = ?");
			ps.setString(1,xh);
			
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(count.trim().equals("1")){
					return true;
				}else{
					return false;
				}
			}
			 
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	public DaduiJdyj getJdyjByXh(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		DaduiJdyj jdyj = null;
		
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from dd_jdyj where xh = ?");
			ps.setString(1,xh);
			
			rs = ps.executeQuery();
			if(rs.next())
			{  
				jdyj = new DaduiJdyj();
				jdyj.setId(Integer.parseInt(rs.getString("id")));
				jdyj.setXh(rs.getString("xh"));
				jdyj.setRq(rs.getString("rq"));
				jdyj.setYj(rs.getString("yj"));
				jdyj.setZdyXm(rs.getString("xm"));
			}
			 
			return jdyj;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return null;
	}

	public boolean saveDaduiJdyj(DaduiJdyj jdyj) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into dd_jdyj(xh,yj,xm,rq) values(?,?,?,?)");
			 
			ps.setString(1, jdyj.getXh());
			ps.setString(2, jdyj.getYj());
			ps.setString(3, jdyj.getZdyXm());
			ps.setString(4, jdyj.getRq());
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return  false;
	}

	public boolean updateDaduiJdyj(DaduiJdyj jdyj) {
		Connection con = null;
		Statement st = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			
			st = con.createStatement();
			String sql1 = "update dd_jdyj set xm = \'" + jdyj.getZdyXm() + "\' where xh = "+jdyj.getXh();
			String sql2 = "update dd_jdyj set rq = \'" + jdyj.getRq() + "\' where xh = "+jdyj.getXh();
			String sql3 = "update dd_jdyj set yj = \'" + jdyj.getYj() + "\' where xh = "+jdyj.getXh();
			 
		    st.executeUpdate(sql1);
		    st.executeUpdate(sql2);
		    st.executeUpdate(sql3);
		  
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(st, con);
		}
		return false;
	}
}
